package com.suse.service.view.order.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.model.dto.CourseDto;
import com.suse.model.dto.UserDto;
import com.suse.service.view.order.entity.Order;
import com.suse.service.view.order.feign.ViewCourseService;
import com.suse.service.view.order.feign.ViewUserService;
import com.suse.service.view.order.mapper.OrderMapper;
import com.suse.service.view.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suse.service.view.order.util.OrderUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private ViewCourseService viewCourseService;

    @Resource
    private ViewUserService viewUserService;

    @Resource
    private OrderMapper orderMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveOrder(String courseId, String userId) {
        Order order = lambdaQuery()
                .eq(Order::getCourseId, courseId)
                .eq(Order::getUserId, userId)
                .one();
        if (ObjectUtil.isNotNull(order)) {
            return R.put("orderId",order.getId()).success();
        }
        CourseDto courseDto = viewCourseService.getCourseDtoById(courseId);
        if (ObjectUtil.isNull(courseDto)) {
            throw new ApiException("未获取到课程信息，请稍后重试！");
        }
        UserDto userDto = viewUserService.getUserDtoById(userId);
        if (ObjectUtil.isNull(userDto)) {
            throw new ApiException("未获取到用户信息，请稍后重试！");
        }
        //创建订单
        order = new Order();
        order.setOrderNo(OrderUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherName());
        order.setPrice(courseDto.getPrice());
        order.setUserId(userId);
        order.setPhone(userDto.getPhone());
        order.setNickname(userDto.getNickname());
        order.setStatus(0);//未支付
        order.setPayType(1);//微信支付
        if (save(order)) {
            String orderId = order.getId();
            return R.put("orderId",orderId).success();
        }else {
            throw new ApiException("订单创建失败！");
        }
    }

    @Override
    public Order getOrderInfoById(String id,String userId) {
        return lambdaQuery()
                .eq(Order::getId, id)
                .eq(Order::getUserId, userId)
                .one();
    }

    @Override
    public boolean hasBuy(String courseId, String userId) {
        Order order = lambdaQuery()
                .select(Order::getStatus)
                .eq(Order::getCourseId, courseId)
                .eq(Order::getUserId, userId)
                .one();
        // 没有订单记录 没有购买课程
        if (ObjectUtil.isNull(order)) {
            return false;
        }
        return order.getStatus() == 1;
    }

    @Override
    public R getOrderList(String userId) {
        List<Order> orderList = orderMapper.getMyOrderList(userId);
        // 为什么查不出来？？？？
        // List<Order> orderList = lambdaQuery()
        //         .eq(Order::getUserId, userId)
        //         .orderByDesc(Order::getGmtCreate)
        //         .list();
        return R.put("orderList",orderList).success();
    }

    @Override
    public R removeMyOrder(String orderId, String userId) {
        boolean remove = lambdaUpdate()
                .eq(Order::getId, orderId)
                .eq(Order::getUserId, userId)
                .remove();
        if (remove) {
            return R.success("删除成功");
        }else {
            throw new ApiException("删除失败！");
        }
    }

    @Override
    public void updateStatusByOrderNo(String orderNo,Integer status) {
        Order order = new Order();
        order.setStatus(1);
        lambdaUpdate()
                .eq(Order::getOrderNo,orderNo)
                .update(order);
    }

}
